package com.zhejiangzhengyuan.municipal_common.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.JSONPath;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhejiangzhengyuan.municipal_common.entity.LED.SendTextJson;
import com.zhejiangzhengyuan.municipal_common.entity.LedHistory;
import com.zhejiangzhengyuan.municipal_common.mapper.LedHistoryMapper;
import com.zhejiangzhengyuan.municipal_common.service.LedHistoryService;
import com.zhejiangzhengyuan.municipal_common.utils.LED.DeviceUtils;
import com.zhejiangzhengyuan.municipal_common.utils.LED.HttpClientUtils;
import com.zhejiangzhengyuan.municipal_common.utils.LED.URLUtils;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.logging.log4j.util.Strings;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 服务实现类
 *
 * @author linju
 * @since 2020-01-12
 */
@Service
public class LedHistoryServiceImpl extends ServiceImpl<LedHistoryMapper, LedHistory>
    implements LedHistoryService {
  @Autowired private URLUtils urlUtils;
  @Autowired private SendTextJson sendTextJson;

  @Override
  public int sendLedData(String mac, JSONObject data) {
    Map<String, String> map = new HashMap<>();
    Document document = null;
    String deviceCode = null;
    try {
      document = Jsoup.connect(urlUtils.GetDeviceMainURL()).get();
      List<Map<String, String>> mapList = DeviceUtils.GetDeviceData(document);
      if (Objects.nonNull(mapList) && !mapList.isEmpty()) {
        for (Map<String, String> map1 : mapList) {
          if (map1.get("MAC").equals(mac)) {
            deviceCode = map1.get("DeviceCode");
          }
        }
      }
    } catch (IOException e) {
      e.printStackTrace();
    }
    if (Strings.isBlank(deviceCode)) {
      return HttpStatus.SC_BAD_REQUEST;
    }
    map.put("devCode", deviceCode);
    map.put("strProgramList", data.toJSONString());
    CloseableHttpResponse response = HttpClientUtils.doPost(urlUtils.GetsendDataURL(), map);
    if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
      LedHistory ledHistory = new LedHistory();
      ledHistory.setDeviceCode(deviceCode);
      // 获取mac地址
      ledHistory.setDeviceMac(mac);
      ledHistory.setMessageContent(data.toJSONString());
      ledHistory.setTextContent(
          String.valueOf(
              JSONPath.read(
                  data.toJSONString(), "$page[0].areaList.mediaEle[0].textGroup[0].text")));
      ledHistory.setTaskType("150"); // 150 指令为发送数据
      save(ledHistory);
    }
    return response.getStatusLine().getStatusCode();
  }

  @Override
  public int sendLedData(String deviceCode, String mac, String data) {
    Map<String, String> map = new HashMap<>();
    map.put("devCode", deviceCode);
    map.put("strProgramList", data);
    CloseableHttpResponse response = HttpClientUtils.doPost(urlUtils.GetsendDataURL(), map);
    if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
      LedHistory ledHistory = new LedHistory();
      ledHistory.setDeviceCode(deviceCode);
      ledHistory.setDeviceMac(mac);
      ledHistory.setMessageContent(data);
      ledHistory.setTextContent(
          String.valueOf(JSONPath.read(data, "$page[0].areaList.mediaEle[0].textGroup[0].text")));
      ledHistory.setTaskType("150"); // 150 指令为发送数据
      save(ledHistory);
    }
    return response.getStatusLine().getStatusCode();
  }

  private JSONObject assignments(String text) {
    JSONObject requestData = sendTextJson.senText;
    JSONPath.set(requestData, "$page[0].areaList.mediaEle[0].textGroup[0].text", text);
    return requestData;
  }
}
